﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TJADRICD.Model;
using TJADRICD.BLL;

using DNA;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using DBTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;

namespace TJADRICD.CollaborativeDesignPlugin.Form
{
    public partial class FormSettingFrameInfo : System.Windows.Forms.Form
    {
        private string insertOrSetOrUpdateOrVirtualFlag;//用来标记是插入图框信息调用，还是设置图框信息调用
        private VersionNumber vn;//更新版本信息所用的实体
        public FrameInfo frameInfo;
        public string strCTB;
        public bool set = false;
        public FormSettingFrameInfo()
        {
            InitializeComponent();
            frameInfo = new FrameInfo();
        }

        private string MakeDigiString(int digi, int length)
        {
            string temp = "";
            int divider = 1;
            for (int count = 0; count < length - 1; ++ count)
            {
                divider *= 10;
            }
            for (int count = 0; count < length; ++ count )
            {
                temp += (digi / divider).ToString();
                digi %= divider;
                divider /= 10;
            }
            return temp;
        }

        public FormSettingFrameInfo(FrameInfo frameInfo, bool setOrNot, List<string> listCTB, string strCTB, bool first,string insertOrSetOrUpdateOrVirtualFlag)
        {
            InitializeComponent();
//             cb_TK.Items.Add("A0");
//             cb_TK.Items.Add("A1");
//             cb_TK.Items.Add("A2");
            this.insertOrSetOrUpdateOrVirtualFlag=insertOrSetOrUpdateOrVirtualFlag;//判断formSettingFrameInfo应该响应哪种操作.
            VersionInfo vi ;//= new VersionInfo();
            frameInfo.versionList.versionList.TryGetValue(frameInfo.versionList.versionList.Count - 1, out vi);
            if(vi==null) 
            {
                vi = new VersionInfo();
            }
            tbDescriptionChinese.Text = vi.descriptionChinese; 
            tbDescriptionEnglish.Text = vi.descriptionEnglish;
            tbClientChinese.Text = frameInfo.ClientChinese;
            tbClientEnglish.Text = frameInfo.ClientEnglish;
            tbProjectNameChinese.Text = frameInfo.ProjectNameChinese; 
            tbProjectNameEnglish.Text = frameInfo.ProjectNameEnglish;
            tbSubProjectChinese.Text = frameInfo.SubProjectChinese;
            tbSubProjectEnglish.Text = frameInfo.SubProjectEnglish;
            tbProjectNo.Text = frameInfo.ProjectNo;
            int verCount = frameInfo.versionList.versionList.Count;
            //这里是我要修改的任务
            //Modify Date:2012-3-20;
            //Function:恢复这段代码功能
            //Relation Documents:modify2012-3-20.doc
            if (verCount == 0)
            {
                this.tbVerNo.Text = "A";
            }
            else
            {
                String str = "";
                str += (char)(verCount - 1 + 'A');
                this.tbVerNo.Text = str;
            }
            
            /*
             * Modify Date:2011-12-8;
             * Modify Author:ChenZhiDong;
             * Reason:version should be read from database;
             * */
            //begin
            //判断图纸信息是否存在,版本号操作的过程
            /*Again Modify Date:2012-3-20;
             * Modify Author:ChenZhiDong;
             * Reason:Cancel the settingframe fuction,only reserve the the insertingframe fuction; 
             * Opertion:Cancel the paragraph code;
             * Relation Documents:modify2012-3-20.doc
             */
            if (this.insertOrSetOrUpdateOrVirtualFlag.Equals(CONSTCOMMAND.SETSELECTEDFRAME))// if (this.insertOrSetOrUpdateOrVirtualFlag.Equals(CONSTCOMMAND.LOADINSERTSET)||this.insertOrSetOrUpdateOrVirtualFlag.Equals(CONSTCOMMAND.SETSELECTEDFRAME))曾经这个
            {
                VersionNumberService vns = new VersionNumberService();
                //vn belongs to a kind of class that is sealed
                vn = vns.getVersionNumberInfo(frameInfo.ProjectNo, frameInfo.SubProjectNo, frameInfo.SheetTitleChinese, frameInfo.DisciplineChinese, frameInfo.StageChinese, frameInfo.SheetNo);
                if (null == vn)
                {
                    if (verCount == 0)            //这对if和else主要是兼容窗口，版本升级功能.
                    {
                        this.tbVerNo.Text = "A";
                    }
                    else
                    {
                        String str = "";
                        str += (char)(verCount - 1 + 'A');
                        this.tbVerNo.Text = str;
                    }
                }
                else
                {
                    if (this.insertOrSetOrUpdateOrVirtualFlag.Equals(CONSTCOMMAND.LOADINSERTSET))//经过测试这条语句永远不会成立，因为插入时只能获得pn和spn的值，其他的无法获得.
                    {
                        MessageBox.Show("此图纸已经存在一个版本，请通过设置图框信息进行操作！");
                        return;
                    }
                    if (vn.Audit)
                    {
                        this.tbVerNo.Text = vns.getNewVersionNumber(vn.VersionNo).ToString();
                    }
                    else
                    {
                       // MessageBox.Show(vn.VersionNo.ToString());
                        this.tbVerNo.Text = vn.VersionNo.ToString();
                    }
                }

            }
            else        //这一部分主要是对程序中，其他部分可能用到版本号的功能做了原来功能逻辑的保存，而非我的逻辑思想
            {
                if (verCount == 0)
                {
                    this.tbVerNo.Text = "A";
                }
                else
                {
                    String str = "";
                    str += (char)(verCount - 1 + 'A');
                    this.tbVerNo.Text = str;
                }
            }
           //end
            tbSubProjectNo.Text = frameInfo.SubProjectNo;
            tbSheetTitleChinese.Text = frameInfo.SheetTitleChinese;
            tbSheetTitleEnglish.Text = frameInfo.SheetTitleEnglish;
            cbDisciplineChinese.Text = frameInfo.DisciplineChinese;

            cbDisciplineEnglish.Text = frameInfo.DisciplineEnglish;
            cbStageChinese.Text = frameInfo.StageChinese;
            cbStageEnglish.Text = frameInfo.StageEnglish;
            tbSheetNo.Text = frameInfo.SheetNo;
            cb_TK.Text = frameInfo.frameType;
            tb_Scale.Text = frameInfo.scale.ToString();

            bool b = true;
            foreach (string str in listCTB)
            {
                if (str == frameInfo.strCTB)
                {
                    b = false;
                    cb_ctb.Text = str;
                }
                cb_ctb.Items.Add(str);
            }
            if (strCTB == "null")
            {
                b = false;
                cb_ctb.SelectedIndex = 0;
            }
            if (b && !first)
            {
                //MessageBox.Show("图纸打印样式欠缺，以选择为默认样式！");
                cb_ctb.SelectedIndex = 0;
            }

            this.frameInfo = frameInfo;
            this.strCTB = strCTB;

            if (first)
            {
                tbDescriptionChinese.Text = frameInfo.DescriptionChinese;
                tbDescriptionEnglish.Text = frameInfo.DescriptionEnglish;
                if (tbSheetNo.Text != "")
                {
                    string temp = tbSheetNo.Text;
                    int digiLength = 0;
                    for (int iCount = temp.Length - 1; iCount >= 0; --iCount)
                    {
                        if ((temp[iCount] >= '0') && (temp[iCount] <= '9'))
                        {
                            ++digiLength;
                        }
                    }
                    temp = temp.Substring(temp.Length - digiLength, digiLength);
                    int numberBuffer = Convert.ToInt32(temp);
                    ++numberBuffer;
                    tbSheetNo.Text = tbSheetNo.Text.Substring(0, tbSheetNo.Text.Length - digiLength) + MakeDigiString(numberBuffer, digiLength);
                }
            }

            if (setOrNot)
            {
                tbClientChinese.Enabled = false;
                tbClientEnglish.Enabled = false;
                tbProjectNameChinese.Enabled = false;
                tbProjectNameEnglish.Enabled = false;
                tbSubProjectChinese.Enabled = false;
                tbSubProjectEnglish.Enabled = false;
                tbProjectNo.Enabled = false;
                tbSubProjectNo.Enabled = false;
            }
            else
            {
                tbDescriptionChinese.Enabled = false;
                tbDescriptionEnglish.Enabled = false;
                tbSheetTitleChinese.Enabled = false;
                tbSheetTitleEnglish.Enabled = false;
                cbDisciplineChinese.Enabled = false;
                cbDisciplineEnglish.Enabled = false;
                cbStageChinese.Enabled = false;
                cbStageEnglish.Enabled = false;
                tbSheetNo.Enabled = false;
            }

            if (!first)
            {
                cb_TK.Visible = false;
                tb_Scale.Visible = false;
                label1.Visible = false;
                label3.Visible = false;
            }
        }

        private void FormSettingFrameInfo_Load(object sender, EventArgs e)
        {
            ;
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tb_Scale_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar);
        }

        private void cbDisciplineChinese_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbDisciplineEnglish.SelectedIndex = cbDisciplineChinese.SelectedIndex;
        }

        private void cbDisciplineEnglish_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbDisciplineChinese.SelectedIndex = cbDisciplineEnglish.SelectedIndex;
        }

        private void cbStageChinese_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbStageEnglish.SelectedIndex = cbStageChinese.SelectedIndex;
        }

        private void cbStageEnglish_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbStageChinese.SelectedIndex = cbStageEnglish.SelectedIndex;
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
        }
        
        private void btn_OK_Click_1(object sender, EventArgs e)
        {
            if (tbDescriptionChinese.Text == "")
            {
                MessageBox.Show("图框信息不完整！\n请完善图框信息!");
                return;
            }
            else if (tbSheetTitleChinese.Text == "")
            {
                MessageBox.Show("图框信息不完整！\n请完善图框信息!");
                return;
            }
            else if (cbDisciplineChinese.Text == "")
            {
                MessageBox.Show("图框信息不完整！\n请完善图框信息!");
                return;
            }
            else if (cbStageChinese.Text == "")
            {
                MessageBox.Show("图框信息不完整！\n请完善图框信息!");
                return;
            }
            else if (tbSheetNo.Text == "")
            {
                MessageBox.Show("图框信息不完整！\n请完善图框信息!");
                return;
            }
            else if (cb_ctb.Text == "")
            {
                MessageBox.Show("打印样式未选择！\n请选择打印样式!");
                return;
            }

            /*
             *Modify Date:2011-12-9
             *Author:ChenZhiDong
             *Function:Finish VersionNumber Update
             * */
            //begin
            //只有设置图框信息的时候，才进行数据库信息更新，即图纸版本号更新

            /*Again Modify Date:2012-3-20;
            * Modify Author:ChenZhiDong;
            * Reason:Cancel the settingframe fuction,only reserve the the insertingframe fuction; 
            * Opertion:Cancel the paragraph code;
            * Relation Documents:modify2012-3-20.doc
            */

            /*if(this.insertOrSetOrUpdateOrVirtualFlag.Equals(CONSTCOMMAND.SETSELECTEDFRAME))
            {
                VersionNumberService vns = new VersionNumberService();
                if (null != vn)
                {
                    if (vn.Audit)   //审核通过才可以进行，更新数据库中图纸版本号.
                    {
                        Boolean isSuccess = vns.updateVersionNumberDataBase(vns.getNewVersionNumber(vn.VersionNo), tbProjectNo.Text, tbSubProjectNo.Text, tbSheetTitleChinese.Text, cbDisciplineChinese.Text, cbStageChinese.Text, tbSheetNo.Text);
                        if (!isSuccess)
                        {
                            MessageBox.Show("版本号更新失败,请联系审核员!");
                        }
                    }  
                }
            }*/
           //end
            frameInfo.DescriptionChinese = tbDescriptionChinese.Text;
            frameInfo.DescriptionEnglish = tbDescriptionEnglish.Text;
            frameInfo.ClientChinese = tbClientChinese.Text;
            frameInfo.ClientEnglish = tbClientEnglish.Text;
            frameInfo.ProjectNameChinese = tbProjectNameChinese.Text;
            frameInfo.ProjectNameEnglish = tbProjectNameEnglish.Text;
            frameInfo.SubProjectChinese = tbSubProjectChinese.Text;
            frameInfo.SubProjectEnglish = tbSubProjectEnglish.Text;
            frameInfo.ProjectNo = tbProjectNo.Text;
            frameInfo.SubProjectNo = tbSubProjectNo.Text;
            frameInfo.SheetTitleChinese = tbSheetTitleChinese.Text;
            frameInfo.SheetTitleEnglish = tbSheetTitleEnglish.Text;
            frameInfo.DisciplineChinese = cbDisciplineChinese.Text;
            frameInfo.DisciplineEnglish = cbDisciplineEnglish.Text;
            frameInfo.StageChinese = cbStageChinese.Text;
            frameInfo.StageEnglish = cbStageEnglish.Text;
            frameInfo.SheetNo = tbSheetNo.Text;
            frameInfo.frameType = cb_TK.Text;
            frameInfo.scale = Convert.ToInt32(tb_Scale.Text);

            frameInfo.strCTB = cb_ctb.Text;

            /*
          *Modify Date:2012-3-18;
          *Author:ChenZhiDong;
          *Function:Insertion Frame function should insert datas into the database;
          * */
            //begin
            if (this.insertOrSetOrUpdateOrVirtualFlag.Equals(CONSTCOMMAND.LOADINSERTSET))
            {
                insertVnNumberDatabase(frameInfo.ProjectNo, frameInfo.SubProjectNo, frameInfo.SheetTitleChinese, frameInfo.DisciplineChinese, frameInfo.StageChinese, frameInfo.SheetNo);
            }
            //end

        /*
         * Modify Date:2012-4-15;
         *Author:ChenZhiDong;
         *Function:Setting Frame function should update datas into the database;
         * */
            //begin
            if (this.insertOrSetOrUpdateOrVirtualFlag.Equals(CONSTCOMMAND.SETSELECTEDFRAME))
            {
                setVnNumberDatabase(frameInfo.ProjectNo, frameInfo.SubProjectNo, frameInfo.SheetTitleChinese, frameInfo.DisciplineChinese, frameInfo.StageChinese, frameInfo.SheetNo);
            }
            //end
            set = true;
            this.Close();
        }

        //插入功能响应并操作数据库
        public void insertVnNumberDatabase(string pn,string spn,string stc,string dc,string sc,string sn)
        {
                VersionNumberService vns = new VersionNumberService();
                 Boolean isExistVersionNumber = vns.isExistVersionNumberDataBase(pn,spn,stc,dc,sc,sn);
                 if (isExistVersionNumber)
                 {
                     
                     VersionNumber insert_vn = new VersionNumber();
                     insert_vn = vns.getVersionNumberInfo(pn,spn,stc,dc,sc,sn);
                     if (insert_vn.Audit)
                     {
                         MessageBoxButtons messageButtons_1=MessageBoxButtons.OKCancel;
                         DialogResult dr_1 = MessageBox.Show("图纸信息已审核，若版本升级请按确定，否则取消，重新设置图纸编号！", "图纸版本信息提示框", messageButtons_1);
                         if (dr_1 == DialogResult.OK)
                         {
                             if (vns.insertVersionNumberDataBase(vns.getNewVersionNumber(insert_vn.VersionNo), pn, spn, stc, dc, sc, sn))
                             {
                                 MessageBox.Show("图纸版本信息插入成功!");
                             }
                             else
                             {
                                 MessageBox.Show("图纸版本信息插入失败!");
                             }
                         }
                         else
                         {
                             return;
                         }
                  
                     }
                     else
                     {
                         MessageBoxButtons messageButtons_2 = MessageBoxButtons.OKCancel;
                         DialogResult dr_2 = MessageBox.Show("当前版本信息存在并且未审核，是否将其覆盖?","版本信息存在提示框",messageButtons_2);
                         if (dr_2 == DialogResult.OK)
                         {
                             //不用做任何事情
                         }
                         else
                         {
                            return;
                         }
                     }
                 }
                 else
                 {
                     if (vns.insertVersionNumberDataBase('A', pn, spn, stc, dc, sc, sn))
                     {
                         MessageBox.Show("图纸版本信息插入成功!");
                     }
                     else
                     {
                         MessageBox.Show("图纸版本信息插入失败!");
                     }
                 }
        }
        //设置功能响应并操作数据库
        public void setVnNumberDatabase(string pn, string spn, string stc, string dc, string sc, string sn)
        {
            MessageBoxButtons boxButtons = MessageBoxButtons.OKCancel;
            DialogResult dr = MessageBox.Show("确定更新图纸信息？", "更新图纸信息系统", boxButtons);
            if (dr == DialogResult.OK)
            {
                VersionNumberService vns = new VersionNumberService();
                Boolean isExistVersionNumber = vns.isExistVersionNumberDataBase(pn, spn, stc, dc, sc, sn);
                if (isExistVersionNumber)
                {
                    VersionNumber insert_vn = new VersionNumber();
                    insert_vn = vns.getVersionNumberInfo(pn, spn, stc, dc, sc, sn);
                    if (insert_vn.Audit)
                    {
                        if (vns.insertVersionNumberDataBase(vns.getNewVersionNumber(insert_vn.VersionNo), pn, spn, stc, dc, sc, sn))
                        {
                            MessageBox.Show("图纸版本信息插入成功!");
                        }
                        else
                        {
                            MessageBox.Show("图纸版本信息插入失败!");
                        }

                    }
                    else
                    {
                        //不用做任何事情
                    }
                }
                else
                {
                    if (vns.insertVersionNumberDataBase('A', pn, spn, stc, dc, sc, sn))
                    {
                        MessageBox.Show("图纸版本信息插入成功!");
                    }
                    else
                    {
                        MessageBox.Show("图纸版本信息插入失败!");
                    }
                }
            }
            else
            {
                return;
            }
        }
    }
}
